//
//  UIColor+Extension.swift
//  直播架构
//
//  Created by 我演示 on 2019/10/30.
//  Copyright © 2019 ALLSall. All rights reserved.
//




import UIKit

extension UIColor {

    convenience init(rg: CGFloat , g : CGFloat , b : CGFloat , alpha : CGFloat = 1.0 ){

        self.init(red: rg, green: g, blue: b, alpha: alpha)
    }

   
    class func randomColor() -> UIColor {
        return   UIColor(rg: CGFloat(arc4random_uniform(256)), g: CGFloat(arc4random_uniform(256)), b: CGFloat(arc4random_uniform(256)))
    }
     static func initWithHex(hexString hex: String, alpha:CGFloat = 1) -> UIColor {
            // 去除空格等
            var cString: String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).uppercased()
            // 去除#
            if (cString.hasPrefix("#")) {
                cString = (cString as NSString).substring(from: 1)
            }
            // 必须为6位
            if (cString.count != 6) {
                return UIColor.gray
            }
            // 红色的色值
            let rString = (cString as NSString).substring(to: 2)
            let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
            let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
            // 字符串转换
            var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0
            Scanner(string: rString).scanHexInt32(&r)
            Scanner(string: gString).scanHexInt32(&g)
            Scanner(string: bString).scanHexInt32(&b)

            return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: alpha)
        }

}
